package org.yufan.mq;

import org.apache.activemq.command.ActiveMQTextMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.yufan.common.JedisService;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;

/**
 * Created by zxd on 2018/4/8.
 */
public class CustomerMessageListener implements MessageListener{

    private Logger LOGGER= LoggerFactory.getLogger(CustomerMessageListener.class);

    private JedisService jedisService;

    @Override
    public void onMessage(Message message) {
        //接收商品id的消息，删除缓存
        ActiveMQTextMessage am=(ActiveMQTextMessage) message;
        try {
            String productId=am.getText();
            if(StringUtils.isEmpty(productId)){
                LOGGER.info("接收到的商品id为空{}",productId);
                return;
            }
            LOGGER.info("接收到的商品id是{}",productId);
            //删除缓存
            jedisService.hdel("item",productId);
            LOGGER.info("删除了商品{}缓存",productId);
         } catch (JMSException e) {
            LOGGER.error("has a exception {}",e);
            e.printStackTrace();
        }


    }


    public JedisService getJedisService() {
        return jedisService;
    }

    public void setJedisService(JedisService jedisService) {
        this.jedisService = jedisService;
    }
}
